<!-- -->
<template>
  <div id='product'>
      <div class="tradetop">商品购买</div>
      <div class="productlist">
          <div class="product_box" v-for="(item,index) in product" :key="index">
              <div class="product_top"><img :src="'http://ebuc.sosocnc.com/Public/'+item.goods_img" alt="" srcset=""></div>
              <div class="product_center">
                  <div class="left">{{item.goods_name}}</div>
                  <div class="right">{{item.goods_price}}</div>
              </div>
              <div class="product_center product_foot">
                  <div class="left">购买数量</div>
                  <div class="right">
                      <div class="minus" @click="minus(index)">-</div>
                      <div class="blank">{{item.count}}</div>
                      <div class="add" @click="add(index)">+</div>
                  </div>
              </div>
          </div>
          <div class="product_order">
              <div class="box1"><span>合计：{{sumprice}}</span><span>（共计 {{sumnumber}} 件）</span></div>
              <div class="btn">
                <button @click="tradesellalert">立即购买</button>
              </div>
          </div>
      </div>
      <!-- 弹出窗口 -->
    <el-dialog title="下单信息" :visible.sync="dialogFormVisible" :close-on-click-modal=false style="width:70%;min-width:50%;margin-left:15vw;">
        <div class="alertmes">
            <ul>
                <li>合计价格：&emsp;<span>{{sumprice}}</span></li>
                <li>商品数量：&emsp;<span>{{sumnumber}}</span></li>                
                <li>收货人：&emsp;&emsp;<input type="text" v-model="name" placeholder="请输入你的名字">&emsp;</li>
                <li>手机号：&emsp;&emsp;<input type="text" v-model="phone" placeholder="请输入你的手机号">&emsp;</li>
                <li>收货地址：&emsp;<textarea style="resize:none" rows="3" cols="28" v-model="address" required placeholder="请输入你的收货地址"></textarea>
                <li>安全密码：&emsp;<input type="password" v-model="password" placeholder="请输入你的安全密码">&emsp;</li>
            </ul>
            <div class="btn">
                <button @click="zhifu">确定支付</button>
                <button @click="zhifuclose">取消</button>
            </div>
            <!-- <div class="notice">*下单后买房需在30分钟之内完成支付，过时将自动取消交易。</div> -->
        </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "product",
  data() {
    return {
      dialogFormVisible: false,
      arr: [],
      name: "",
      phone: "",
      address: "",
      password: "",
      product: []
    };
  },
  computed: {
    sumprice: function() {
      var total = 0;
      // var num = 0;
      var k = this.product.length;
      for (var i = 0; i < k; i++) {
        total += this.product[i].goods_price * this.product[i].count;
      }
      return total;
      //每隔三位加一个逗号
      // return total.toString().replace(/\B(?=(\d{3})+$)/g, ",");
    },
    sumnumber: function() {
      var num = 0;
      var j = this.product.length;
      for (var i = 0; i < j; i++) {
        num += parseInt(this.product[i].count);
      }
      return num;
    }
  },

  mounted() {},

  mounted() {
    this.loadmes();
  },

  methods: {
    // 商品列表
    loadmes() {
      this.axios
        .post("User/goods_list")
        .then(({ data }) => {
          console.log(data);
          this.product = data.goods;
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    tradesellalert() {
      if (this.sumnumber === 0) {
        this.$message({ message: "请选择商品", type: "error" });
      } else {
        this.dialogFormVisible = true;
        this.product.forEach((element, index) => {
          if (this.product[index].count > 0) {
            this.arr.push({
              id: this.product[index].goods_id,
              count: this.product[index].count
            });
          }
        });
      }
    },
    tradesellclose() {
      this.dialogFormVisible = false;
    },
    // 确定支付
    zhifu() {
      if (!this.name) {
        this.$message({ message: "收货人不能为空", type: "error" });
      } else if (!this.address) {
        this.$message({ message: "收货地址不能为空", type: "error" });
      } else if (!this.phone) {
        this.$message({ message: "联系方式不能为空", type: "error" });
      } else if (!this.password) {
        this.$message({ message: "请输入密码", type: "error" });
      } else {
        this.axios
          .post("User/pay_goods_cl", {
            arr: this.arr,
            money: this.sumprice,
            shr: this.name,
            address: this.address,
            phone: this.phone,
            password: this.password
          })
          .then(({ data }) => {
            console.log(data);
            if (data.status === "1") {
              this.$message({ message: "密码错误", type: "error" });
            } else if (data.status === "2") {
              this.$message({ message: "积分不足", type: "error" });
            } else if (data.status === "4") {
              this.$message({ message: "操作失败", type: "error" });
            } else if (data.status === "3") {
              this.dialogFormVisible = false;
              this.$router.push("myorder");
              this.$bus.$emit("loadebuc");
              this.$message({ message: "操作成功", type: "success" });
            }
          })
          .catch(function(error) {
            console.log(error);
          });
      }
    },
    zhifuclose() {
      this.dialogFormVisible = false;
    },
    minus(index) {
      if (this.product[index].count > 0) {
        this.product[index].count--;
      }
    },
    // 加商品的数量
    add(index) {
      this.product[index].count++;
    }
  }
};
</script>

<style lang='scss' scoped>
#product {
  height: 100%;
  overflow: auto;
  padding-top: 55px;
  padding-bottom: 110px;
  box-sizing: border-box;
  overflow-y: visible;
  .tradetop {
    width: 100%;
    height: 55px;
    line-height: 65px;
    font-size: 13px;
    font-family: MicrosoftYaHei-Bold;
    color: rgba(0, 0, 0, 1);
    font-weight: 700;
    padding-left: 25px;
    box-sizing: border-box;
    border-bottom: 1px solid rgba(153, 153, 153, 1);
    position: fixed;
    top: 50px;
    right: 20px;
    left: 270px;
    background-color: #fff;
    z-index: 99;
  }
  .productlist {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    .product_box {
      width: 280px;
      height: 378px;
      opacity: 0.99;
      margin-left: 20px;
      margin-top: 25px;
      border: 1px solid rgba(221, 221, 221, 1);
      .product_top {
        width: 280px;
        height: 280px;
        overflow: hidden;
        img {
          width: 100%;
        }
      }
      .product_center {
        padding: 0 15px;
        display: flex;
        justify-content: space-between;
        line-height: 40px;
        font-size: 16px;
        font-family: PingFangSC-Medium;
        color: rgba(0, 0, 0, 1);
        box-sizing: border-box;
        .right {
          color: #ff0000;
        }
      }
      .product_foot {
        display: flex;
        align-items: center;
        justify-content: space-between;
        font-size: 14px;
        .right {
          display: flex;
          align-items: center;
          //   width: 105px;
          height: 25px;
          line-height: 25px;
          font-size: 15px;
          color: #666;
          border: 1px solid rgba(153, 153, 153, 1);
          .add,
          .minus {
            cursor: pointer;
            width: 25px;
            line-height: 25px;
            height: 25px;
            // border-radius: 10px;
            text-align: center;
            color: #9290ae;
            &:hover {
              background: #e5e5e5;
            }
          }
          .blank {
            width: 55px;
            line-height: 25px;
            height: 25px;
            text-align: center;
            color: #282828;
            border-left: 1px solid rgba(153, 153, 153, 1);
            border-right: 1px solid rgba(153, 153, 153, 1);
          }
        }
      }
    }
    .product_order {
      //   width: 100%;
      height: 59px;
      display: flex;
      align-items: center;
      justify-content: flex-start;
      background: rgba(255, 255, 255, 0.8);
      box-shadow: 0px -1px 3px 0px rgba(0, 0, 0, 0.5);
      position: absolute;
      bottom: 10px;
      right: 20px;
      left: 20px;
      padding-left: 30px;
      box-sizing: border-box;
      span:nth-of-type(1) {
        font-size: 20px;
        font-family: PingFangSC-Medium;
        font-weight: 500;
        color: rgba(224, 3, 0, 1);
      }
      span:nth-of-type(2) {
        font-size: 15px;
        font-family: PingFangSC-Regular;
        font-weight: 400;
        color: rgba(0, 0, 0, 1);
      }
      button {
        margin-left: 30px;
        cursor: pointer;
        width: 82px;
        height: 33px;
        background: rgba(224, 3, 0, 1);
        font-size: 12px;
        font-family: MicrosoftYaHei;
        color: rgba(255, 255, 255, 1);
        border-radius: 2px;
      }
    }
  }
  .alertmes {
    margin-left: 40px;
    margin-top: -30px;
    li {
      line-height: 50px;
      font-size: 12px;
      font-family: MicrosoftYaHei;
      color: rgba(102, 102, 102, 1);
      span {
        font-weight: 700;
      }
    }
    li:nth-of-type(1),
    li:nth-of-type(2) {
      line-height: 30px;
    }
    input {
      width: 212px;
      height: 33px;
      padding-left: 5px;
      border-radius: 2px;
      border: 1px solid rgba(151, 151, 151, 1);
    }
    li:nth-of-type(1) {
      span {
        color: rgba(224, 3, 0, 1);
      }
    }
    .btn {
      margin-top: 30px;
      button {
        cursor: pointer;
        width: 82px;
        height: 33px;
        background: rgba(224, 3, 0, 1);
        font-size: 12px;
        font-family: MicrosoftYaHei;
        color: rgba(255, 255, 255, 1);
        border-radius: 2px;
      }
      button:nth-of-type(2) {
        margin-left: 25px;
        background: rgba(225, 225, 225, 1);
        color: rgba(0, 0, 0, 1);
      }
    }
    .notice {
      font-size: 12px;
      line-height: 40px;
    }
  }
}
</style>